package com.c8a.controller;

import com.c8a.utils.Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @Author: C8A
 * @Date: 2021-09-15 14:44
 */

@Controller
@Api(tags = "主页API")
public class HelloController {

    private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/")
    @ApiOperation(value = "跳转主页")
    public String index(HttpServletRequest request, Model model) {
        Long views = redisTemplate.opsForValue().increment("views");
        model.addAttribute("views", views);
        String ip = Utils.getIP(request);
        model.addAttribute("ip", ip);
        logger.info(ip);
        logger.info(String.valueOf(views));

        return "index";
    }
}
